package or;

import CommonStructure.ListNode;

public class T0023offer2 {
    public ListNode getIntersectionNode(ListNode headA, ListNode headB) {
        ListNode tmpA = headA;
        ListNode tmpB = headB;
        int lenA = 0;
        int lenB = 0;
        while (tmpA != null) {
            lenA++;
            tmpA = tmpA.next;
        }
        while (tmpB != null) {
            lenB++;
            tmpB = tmpB.next;
        }
        tmpA = headA;
        tmpB = headB;
        if (lenA > lenB) {
            int diff = lenA - lenB;
            while (diff > 0) {
                tmpA=tmpA.next;
                diff--;
            }
        }else {
            int diff=lenB-lenA;
            while (diff>0){
                tmpB=tmpB.next;
                diff--;
            }
        }
        while (tmpA!=tmpB){
            tmpA=tmpA.next;
            tmpB=tmpB.next;
        }
        return tmpA;

    }
}
